#include "mandate.h"
int cd(char* dstdir,pathStack_t* pathstack,char* display)
{

    char realdir[128]={0};
    if(pathstack->length == 0)
    {
        PushStack(pathstack,dstdir);
        return 0;
    }
    pwd(pathstack,realdir);
    strcat(realdir,dstdir);

    if(dstdir[0]=='\0')
    {
        while(pathstack->length > 1)
        {
            PopStack(pathstack);
        }
    }
    else
    {
        DIR* is_valid = opendir(realdir);
        NETDISK_LOG_DEBUG
        if(is_valid == NULL)
        {
            NETDISK_LOG_DEBUG
            return -1;
        }
        closedir(is_valid);
        char*tmp = strtok(dstdir,"/");
        while(tmp != NULL)
        {   
            if(strcmp(tmp,".")==0)
            {
                continue;
            }
            if(strcmp(tmp,"..")==0)
            {
                if(pathstack->length >1)
                {
                    PopStack(pathstack);
                    goto end;
                }
                else
                {
                    NETDISK_LOG_DEBUG
                    return -1;
                }
            }
            PushStack(pathstack,tmp);
end:
            tmp = strtok(NULL,"/");
        }
    }
    char dst[128];
    pwd(pathstack,dst);
    char Buf[10]={0};
    int ret = ls(Buf,display,pathstack);
    if(ret == -1)
    {
        NETDISK_LOG_DEBUG
        return -1;
    }
    return 0;
}


int mkdir_server(char* dir_name,pathStack_t* pathstack)
{
    char path[128]={0};
    pwd(pathstack,path);
    strcat(path,dir_name);
    return mkdir(path,0777);
}


int ls(char*path,char *name_list,pathStack_t* pathstack)
{
    char ls_path[100]={0};

    pwd(pathstack,ls_path);
    ls_path[strlen(ls_path)-1]='\0';
    strcat(ls_path,"$");
    strcat(name_list,ls_path);

    memset(ls_path,0,sizeof(ls_path));
    if(path[0]=='\0')
    {
        pwd(pathstack,ls_path);
    }
    else
    {
        char path_temp[100]={0};
        pwd(pathstack,ls_path);
        int ls_ret=cd(path,pathstack,name_list);
        if(ls_ret==-1)
        {
            NETDISK_LOG_DEBUG
                return -1;
        }
        cd(ls_path,pathstack,path_temp);

        return 0;
    }

    DIR *dir=opendir(ls_path);
    if(!dir)
    {
        NETDISK_LOG_DEBUG
        return -1;
    }

    struct dirent*pdirent;
    while((pdirent=readdir(dir)))
    {
        if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0)
        {
            continue;
        }
        strcat(name_list,"\n   |_____");
        strcat(name_list,pdirent->d_name);
    }

        strcat(name_list,"\n\n");
    rewinddir(dir);
    closedir(dir);
    return 0;
}

int pwd(pathStack_t* stack,char* buf){
    Node_t *cur=stack->pTop;

    //暂做修改：start
    pathStack_t* newstack=(pathStack_t *)calloc(1,sizeof(pathStack_t));
    //修改完毕 over
    InitStack(newstack);
    char BUF[]={"/"};
    while(cur){
        PushStack(newstack,BUF);
        PushStack(newstack,cur->dir);
        cur=cur->pNext;
    }//入栈
    while(newstack->length){
        strcat(buf,newstack->pTop->dir);
        PopStack(newstack);
    }//出栈
    return 0;
}

void get_salt(char* salt,char* passwd)
{
    int i = 0,j = 0;
    while(passwd[i]&&j != 3)
    {
        if(passwd[i] == '$')
        {
            ++j;
        }
        ++i;
    }
    strncpy(salt,passwd,i-1);
}

int passwd_check(char* username,char* passwd,char* buf)
{
    struct spwd *passwd_info;
    passwd_info = getspnam(username);
    if(passwd_info == NULL)
    {
        strcpy(buf,"获取系统用户名和密码失败，请检查你的用户名是否正确！\n");
        NETDISK_LOG_DEBUG
        return -1;
    }

    char salt[512] = {0};
    get_salt(salt,passwd_info->sp_pwdp);
    if(strcmp(passwd_info->sp_pwdp,crypt(passwd,salt)) == 0)
    {
        strcpy(buf,"验证通过!");
    }
    else
    {
        printf("22\n");
        strcpy(buf,"验证失败，请检查你的密码！\n");
        NETDISK_LOG_DEBUG
        return -1;
    }
    return 0;

}
